package scr 
{
	import cell.KeyPad;
	import flash.events.Event;
	import flash.text.TextField;
	import img.ScalePlane;
	import ru.gosoplata.model.TmpModel;
	import uicontrols.ButtonBase;
	import usr.ServiseData;
	import usr.WCTehnik;
	/**
	 * ...
	 * @author ...
	 */
	public class ScreenWCLogin extends ScreenBase implements IScreen 
	{
		private var log_field:ScalePlane;
		private var pass_field:ScalePlane;
		private var numpad:KeyPad;
		private var btn_ok:ButtonBase;
		private var btn_exit:ButtonBase;
		private var txt_log:TextField;
		private var txt_pass:TextField;
		private var login:String = "";
		private var password:String = "";
		private var cur_indx:Number;
		private var sdata:ServiseData;
		private var pic_count:Number = 0;
		
		public function ScreenWCLogin() 
		{
			super("wc_login");
		}
		
		override protected function init(e:Event):void 
		{
			super.init(e);
			
			var pl_width:Number = 300;
			log_field = new ScalePlane(pl_width);
			addChild(log_field);
			pass_field = new ScalePlane(pl_width);
			addChild(pass_field);
			numpad = new KeyPad(50);
			addChild(numpad);
			
			createButtons();
		}
		
		private function createButtons():void {
			pic_count = 0;
			btn_ok = new ButtonBase("service_ok");
			btn_ok.addEventListener(ButtonBase.ON_PRESS_UI_BUTTON, pressBtn);
			btn_ok.addEventListener(ButtonBase.ON_PIC_LOADED, allPicLoaded);
			addChild(btn_ok);
			pic_count++;
			
			btn_exit = new ButtonBase("exit");
			btn_exit.addEventListener(ButtonBase.ON_PRESS_UI_BUTTON, pressBtn);
			btn_exit.addEventListener(ButtonBase.ON_PIC_LOADED, allPicLoaded);
			addChild(btn_exit);
			pic_count++;
			
			loadBanner(TmpModel.IMG_FOLDER + "/service/teh_title.png");
		}		
		
		private function allPicLoaded(e:Event):void 
		{
			pic_count--;
			if (pic_count == 0) {
				
				log_field.x = 200;
				log_field.y = 250;
				log_field.addEventListener(ScalePlane.ON_PLANE_CLICK, onPlaneClick);
				txt_log = new TextField();
				txt_log.embedFonts = true;
				txt_log.defaultTextFormat = TmpModel.getTextFormat(40, 0x656565,true);
				txt_log.x = log_field.x + 10;
				txt_log.y = log_field.y - 60;
				txt_log.width = 200;
				txt_log.text = "Логин";
				txt_log.selectable = false;
				addChild(txt_log);
				
				pass_field.x = log_field.x;
				pass_field.y = log_field.y + log_field.height + 20 + 40;
				pass_field.addEventListener(ScalePlane.ON_PLANE_CLICK, onPlaneClick);
				
				txt_pass = new TextField();
				txt_pass.embedFonts = true;
				txt_pass.defaultTextFormat = TmpModel.getTextFormat(40, 0x656565,true);
				txt_pass.x = pass_field.x + 10;
				txt_pass.y = pass_field.y - 60;
				txt_pass.width = 200;
				txt_pass.selectable = false;
				txt_pass.text = "Пароль";
				addChild(txt_pass);
				
				
				numpad.y = log_field.y;
				numpad.x = log_field.x + log_field.width + 150;
				btn_ok.x = log_field.x;
				btn_ok.y = pass_field.y + pass_field.height +20;
				btn_exit.y = stage.stageHeight - btn_exit.height-20;
				btn_exit.x = stage.stageWidth - btn_exit.width - 20;
				numpad.addEventListener(KeyPad.KEY_PAD_RELEASED, reDrawText);
			}
		}		
		
		private function onPlaneClick(e:Event):void 
		{
			var indx:Number = (e.target as ScalePlane).tab_index;
			cur_indx = indx;
			if (cur_indx == 1) {
				numpad.rewrite_value = login;
				log_field.isCheck(true);
				pass_field.isCheck(false);
			}else {
				numpad.rewrite_value = password;
				log_field.isCheck(false);
				pass_field.isCheck(true);
			}
		}
		
		override public function reDraw():void
		{
			log_field.text = login = "";
			pass_field.text = password = "";
			cur_indx = 1;
			log_field.tab_index = 1;
			pass_field.setMaskOnField(8);
			pass_field.tab_index = 2;
			pass_field.isCheck(false);
			numpad.clearBuffer();
			sdata = ServiseData.getInstance();
		}
		
		public function reDrawText(e:Event):void {
			var val:String = (e.target as KeyPad).entered_value;
			if (cur_indx == 1) log_field.text = login = val;
			else pass_field.text = password = val;
		}
		//TODO: переписать!
		private function checkPass():void {
			sdata.currteh = null;
			var teh:WCTehnik = sdata.getTehnikByID(parseInt(login));
			if (teh != null) {
				if (password == teh._pin.toString()) {
					TmpModel.acceptor.wcEnterMenu(teh._id+'');
					sdata.currteh = teh;
					
					TmpModel.tmpGlobalController.onClick('wc_servise');
				}else {
					TmpModel.acceptor.writeLog("ОШИБКА: Техник "+login+" ввел неверный пин: "+password);
					TmpModel.acceptor.wcEnterMenu("0");
					
					TmpModel.tmpGlobalController.onClick('menu');
				}
			}else {
					TmpModel.acceptor.writeLog("ОШИБКА: Неверный логин: "+login);
					TmpModel.acceptor.wcEnterMenu("0");
					
					TmpModel.tmpGlobalController.onClick('menu');
			}
		}
		
		private function pressBtn(e:Event):void 
		{
			switch(e.target.name) {
				case "service_ok":
					checkPass();
					break;

				case "exit":
					TmpModel.tmpGlobalController.onClick('menu');
					break;
			}			
		}	
	}
}